#!/usr/bin/perl
use Getopt::Std;
getopt("s:m:r:w:e", \%args); 
my $sample = $args{s};
my $marker= $args{m};
my $reference= $args{r};
my $window = $args{w};
my $edge = $args{e};
chomp $sample;
chomp $marker;
chomp $reference;
if($sample eq "")
{
	die "entered samplefile name  is empty\n";
	
}
if($marker eq "")
{
        die "entered markerfile name  is empty\n";

}
if($reference eq "")
{
        die "entered referencefile name is empty\n";

}
if($window eq "")
{
        die "entered window size is empty\n";

}
if($edge eq "")
{
        die "entered edge name is empty\n";

}

print "Sample : $sample Marker : $marker Reference: $reference Windowsize : $window Edges : $edge\n";


open BUFF,"<$marker" or die "Can't open $sample : $!";
$n = 1;
while($line = <BUFF>)
{
	chomp $line;
	@array = split(" ",$line);
	$array_coord[$n] = $array[1];
	$n++;
}
@sorted_array_coord = sort { $a <=> $b } @array_coord;
$min = $sorted_array_coord[1];
$max = $sorted_array_coord[@sorted_array_coord-1];
print "Max $max Min $min\n";
my $segment =  sprintf("%d", ($max-$min)/$window);
if($segment < ($max-$min)/$window)
{
	$segment++;
}
#printf "%d\n", 3.1415126;
printf "Number of Segments : $segment\n";
for($i=0;$i<$segment;$i++)
{
	$lowlimit = $min+$window*($i);
	$segment_array_min[$i] =$lowlimit-$edge; 
	$temp = $i+1;
	print "Segment Min ".$temp." : ".$lowlimit."\n";
	$uplimit = $min+$window*($i+1);
	$segment_array_max[$i] =$uplimit+$edge; 
	print "Segment Max ".$temp." : ".$uplimit."\n";
}
print "@segment_array_min\n";
print "@segment_array_max\n";

open BUFF,"<$marker" or die "Can't open $sample : $!";
for($i=0;$i<$segment;$i++)
{
	$num{$i}=0;	

}
open(WR,">no_snp_hapmap.txt");
while($line = <BUFF>)
{
	chomp $line;
        @array = split(" ",$line);
	for($i=0;$i<$segment;$i++)
	{
		if($array[1] > $segment_array_min[$i] && $array[1] < $segment_array_max[$i])
		{
			${"array".$i}[$num{$i}] = $array[0];		
			$num{$i}++;
			$hash{$array[0]} = $i+1;
			
		}	
	}
}
open BUFF,"<$sample" or die "Can't open $sample : $!";
$line1 = <BUFF>;
$line2 = <BUFF>;
for($i=0;$i<$segment;$i++)
{
	$f = $i+1;
	$buff = "BUFF$f";
	$sample=~ s/.bgl/_/g;
	$file = $sample."segment$f".".bgl";
	open($buff,">$file");
	print $buff $line1;
	print $buff $line2;
}
while($line = <BUFF>)
{
	chomp $line;
	@array = split(" ",$line);
	$samplehash{$array[1]} =$line;
	if(exists($hash{$array[1]}))
	{
	#	$num = $hash{$array[1]};
	#	$buff = "BUFF$num";
	#	print $buff $line."\n";
	}
	else
	{
		print WR $line."\n";

	}
}
for($i=0;$i<$segment;$i++)
{
	$f = $i+1;
        $buff = "BUFF$f";
	for($y=0;$y<@{"array".$i};$y++)
	{	if(exists($samplehash{${"array".$i}[$y]}))
		{
		print $buff $samplehash{${"array".$i}[$y]}."\n";
		}
	}
}

open BUFF,"<$reference" or die "Can't open $reference : $!";
$line1 = <BUFF>;
for($i=0;$i<$segment;$i++)
{
        $f = $i+1;
        $buff = "BUFF1$f";
	$reference =~ s/.bgl/_/g;
        $file = $reference."segment$f".".bgl";
        open($buff,">$file");
        print $buff $line1;
}
while($line = <BUFF>)
{
        chomp $line;
        @array = split(" ",$line);
	$referencehash{$array[1]} =$line;
      #          $num = $hash{$array[1]};
       #         $buff = "BUFF$num";
        #        print $buff $line."\n";
}
for($i=0;$i<$segment;$i++)
{
        $f = $i+1;
        $buff = "BUFF1$f";
        for($y=0;$y<@{"array".$i};$y++)
        { 
		if(exists($referencehash{${"array".$i}[$y]}))
		{
                print $buff $referencehash{${"array".$i}[$y]}."\n";
		}

        }
}

open BUFF,"<$marker" or die "Can't open $marker : $!";
for($i=0;$i<$segment;$i++)
{
        $f = $i+1;
        $buff = "BUFF2$f";
        $marker =~ s/.txt/_/g;
        $file = $marker."segment$f".".txt";
        open($buff,">$file");
}
while($line = <BUFF>)
{
        chomp $line;
        @array = split(" ",$line);
	$markerhash{$array[0]} =$line;
              #  $num = $hash{$array[0]};
               # $buff = "BUFF$num";
               # print $buff $line."\n";
}
for($i=0;$i<$segment;$i++)
{
        $f = $i+1;
        $buff = "BUFF2$f";
        for($y=0;$y<@{"array".$i};$y++)
        {
		if(exists($markerhash{${"array".$i}[$y]}))
		{
                print $buff $markerhash{${"array".$i}[$y]}."\n";
		}
        }
}

